package top.hkyzf.util.movement;

import com.mojang.logging.LogUtils;
import net.minecraft.client.player.Input;
import org.slf4j.Logger;
import top.hkyzf.util.OneKeyContext;

/**
 * description: 程序使用的输入类，接管键盘
 * @author zhufeng
 * @date 2022/8/19 17:14
 * @version V1.0
 */
public class PlayerMovementInput extends Input {
    /**
     * 获取一个 slf4j 日志记录器
     */
    private static final Logger LOGGER = LogUtils.getLogger();
    /**
     * 由 LocalPlayer 触发
     * @param isMovingSlowly 是否移动缓慢，应该是按下 shift 的状态，这里不需要
     */
    @Override
    public void tick(boolean isMovingSlowly) {
        this.leftImpulse = 0.0F;
        this.forwardImpulse = 0.0F;
        this.up = isInputPressDown(OneKeyInput.MOVE_FORWARD);
        this.down = isInputPressDown(OneKeyInput.MOVE_BACK);
        this.left = isInputPressDown(OneKeyInput.MOVE_LEFT);
        this.right = isInputPressDown(OneKeyInput.MOVE_RIGHT);
        this.jumping = isInputPressDown(OneKeyInput.JUMP);
        this.shiftKeyDown = isInputPressDown(OneKeyInput.SNEAK);
        if (this.up) {
            this.forwardImpulse++;
        }
        if (this.down) {
            this.forwardImpulse--;
        }
        if (this.left) {
            this.leftImpulse++;
        }
        if (this.right) {
            this.leftImpulse--;
        }
        if (this.shiftKeyDown) {
            this.leftImpulse *= 0.3F;
            this.forwardImpulse *= 0.3F;
        }
    }

    /**
     * 判断传入的输入类型是否被按下
     * @param input AI 输入类型
     * @return true/false 是否被按下
     */
    public final boolean isInputPressDown(OneKeyInput input) {
        return input != null && OneKeyContext.getInstance().getInputPressDownMap().getOrDefault(input, false);
    }
}
